//
//  JSTCommonHeaderView.m
//  KYKJPackDemo
//
//  Created by 01 on 17/5/17.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "JSTCommonHeaderView.h"


@interface JSTCommonHeaderView ()<MLEmojiLabelDelegate>
@end

@implementation JSTCommonHeaderView

- (instancetype)init
{
    self = [super init];
    if (self) {
        _iconView = [[UIImageView alloc] init];
        [self addSubview:_iconView];
        
        _nameLabel = [[UILabel alloc] init];
        [self addSubview:_nameLabel];
        
        _contentLabel = [[MLEmojiLabel alloc] init];
        [self addSubview:_contentLabel];
        
        _lineView = [[UIView alloc] init];
        [self addSubview:_lineView];
        
        _imgContentView = [[UIView alloc] init];
        [self addSubview:_imgContentView];
        
        _timeLabel = [[UILabel alloc] init];
        [self addSubview:_timeLabel];
        
        _commentsBtn = [[UIButton alloc] init];
        [self addSubview:_commentsBtn];
        
        _praiseBtn = [[UIButton alloc] init];
        [self addSubview:_praiseBtn];
        
        _selectView = [[UIView alloc] init];
        [self addSubview:_selectView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changePersonPage)];
        [_iconView addGestureRecognizer:tap];
    }
    return self;
}

/**
 数据填充方法

 @param model 数据模型，含所需数据
 */
- (void)createContent:(JSTInterestcircleDataModel *)model
{
    _model = model;
    
    CGFloat iconX = kCurrentScreenWidth(10);
    CGFloat iconY = iconX;
    CGFloat iconW = kCurrentScreenWidth(30);
    CGFloat iconH = iconW;
    _iconView.frame = CGRectMake(iconX, iconY, iconW, iconH);
    [_iconView setImageWithURL:[NSURL URLWithString:model.iconImgUrl]];
    _iconView.layer.masksToBounds = YES;
    _iconView.layer.cornerRadius = _iconView.width / 2;
    _iconView.userInteractionEnabled = YES;
    
    CGFloat nameX = CGRectGetMaxX(_iconView.frame) + iconX;
    CGSize nameSize = [NSString sizeWithString:model.userName
                                          font:kSystemFont(kCurrentScreenWidth(13))
                                        CGsize:CGSizeMake(kScreenWidth - nameX - iconX, CGRectGetMaxY(_iconView.frame)+iconY)];
    CGFloat nameY = (CGRectGetMaxY(_iconView.frame) + iconY - nameSize.height) / 2;
    _nameLabel.frame = (CGRect){{nameX, nameY}, nameSize};
    _nameLabel.text = model.userName;
    _nameLabel.font = kSystemFont(kCurrentScreenWidth(13));
    
    CGFloat contentX = iconX;
    CGFloat contentY = CGRectGetMaxY(_iconView.frame)+iconY;
    CGSize contentSize = [NSString sizeWithString:model.contentText
                                             font:kSystemFont(kCurrentScreenWidth(10))
                                           CGsize:CGSizeMake(kScreenWidth - iconX*2, MAXFLOAT)];
    _contentLabel.frame = (CGRect){{contentX, contentY}, contentSize};
    _contentLabel.numberOfLines = 0;
    _contentLabel.font = kSystemFont(kCurrentScreenWidth(10));
    _contentLabel.numberOfLines = 0;
    _contentLabel.textInsets = UIEdgeInsetsMake(5, 0, 0, 0);
    _contentLabel.textColor= [UIColor colorWithHexString:@"595757"];
    _contentLabel.emojiDelegate = self;
    _contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
    _contentLabel.isNeedAtAndPoundSign = YES;
    _contentLabel.disableThreeCommon=YES;
    //    _contentLabel.text = model.contentText;
    // _contentLabel.customEmojiRegex =@"[\\[e_]+[0-9]{1,4}+[\\]]";
    _contentLabel.customEmojiRegex = @"\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]";
    _contentLabel.customEmojiPlistName =@"Emoji_icon.plist";
    [_contentLabel setEmojiText:model.contentText];
    [_contentLabel sizeToFit];
    
    CGFloat imgContentX = 0;
    CGFloat imgContentY = CGRectGetMaxY(_contentLabel.frame) + iconY;
    CGFloat imgContentW = kScreenWidth;
    CGFloat imgContentH = ((kScreenWidth - kCurrentScreenWidth(10)) / 3) - kCurrentScreenWidth(10);
    _imgContentView.frame = CGRectMake(imgContentX, imgContentY, imgContentW, imgContentH);
    
    CGFloat lineY;
    if (model.contentImgUrlArr.count == 0) {
        lineY = CGRectGetMaxY(_contentLabel.frame) + iconY;
        [_imgContentView removeFromSuperview];
    } else {
        lineY = CGRectGetMaxY(_imgContentView.frame) + iconY;
        [self createImageView:model.contentImgUrlArr];
    }
    
    CGFloat lineX = 0;
    CGFloat lineW = kScreenWidth;
    CGFloat lineH = 0.35;
    _lineView.frame = CGRectMake(lineX, lineY, lineW, lineH);
    _lineView.backgroundColor = [UIColor lightGrayColor];
    
    CGSize timeSize = [NSString sizeWithString:model.timeText
                                          font:kSystemFont(kCurrentScreenWidth(10))
                                        CGsize:CGSizeMake(kScreenWidth, MAXFLOAT)];
    CGFloat timeX = iconX;
    CGFloat timeY = CGRectGetMaxY(_lineView.frame) + (kCurrentScreenWidth(35) - timeSize.height) / 2;
    _timeLabel.frame = (CGRect){{timeX, timeY}, timeSize};
    _timeLabel.text = model.timeText;
    _timeLabel.font = kSystemFont(kCurrentScreenWidth(10));
    _timeLabel.textColor = [UIColor grayColor];
    
    CGFloat commentX = kScreenWidth / 2;
    CGFloat commentY = CGRectGetMaxY(_lineView.frame);
    CGFloat commentW = (kScreenWidth - iconX * 2) / 4;
    CGFloat commentH = kCurrentScreenWidth(35);
    _commentsBtn.frame = CGRectMake(commentX, commentY, commentW, commentH);
    [_commentsBtn setTitle:[NSString stringWithFormat:@"评论 %@", model.commentNumber] forState:UIControlStateNormal];
    [_commentsBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_commentsBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [_commentsBtn addTarget:self action:@selector(comments) forControlEvents:UIControlEventTouchUpInside];
    _commentsBtn.selected = YES;
    
    CGFloat praiseX = CGRectGetMaxX(_commentsBtn.frame);
    CGFloat praiseY = commentY;
    CGFloat praiseW = commentW;
    CGFloat praiseH = commentH;
    _praiseBtn.frame = CGRectMake(praiseX, praiseY, praiseW, praiseH);
    [_praiseBtn setTitle:[NSString stringWithFormat:@"赞 %@", model.pointPraise] forState:UIControlStateNormal];
    [_praiseBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_praiseBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [_praiseBtn addTarget:self action:@selector(praise) forControlEvents:UIControlEventTouchUpInside];
    
    _selectView.frame = CGRectMake(_commentsBtn.frame.origin.x, CGRectGetMaxY(_praiseBtn.frame) - 4.5, (kScreenWidth/2. - iconX)/2., 4.5);
    _selectView.backgroundColor = [UIColor redColor];
    
    _headerHight = CGRectGetMaxY(_praiseBtn.frame);
    
    if (!_isCommentList) {
        [self praise];
    }
}

- (void)createImageView:(NSArray *)imageArr
{
    [imageArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImageView *imgView = [[UIImageView alloc] init];
        imgView.tag = 100 + idx;
        CGFloat imageX = kCurrentScreenWidth(10) + ((kScreenWidth - kCurrentScreenWidth(10))/3)*idx;
        CGFloat imageY = 0;
        CGFloat imageW = ((kScreenWidth - kCurrentScreenWidth(10))/3) - kCurrentScreenWidth(10);
        CGFloat imageH = imageW;
        imgView.frame = CGRectMake(imageX, imageY, imageW, imageH);
        imgView.userInteractionEnabled = YES;
        
        //描边
        imgView.layer.borderWidth = 0.5;
        imgView.layer.borderColor = [UIColor colorWithHexString:@"e0e0e0"].CGColor;
        
        //设置不拉伸
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        //添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [imgView addGestureRecognizer:tap];
        
        [imgView setImageWithURL:[NSURL URLWithString:obj]];
        [_imgContentView addSubview:imgView];
    }];
}

#pragma mark - 点击图片放大
- (void)tapClick:(UITapGestureRecognizer *)tap
{
    UIImageView *tapImageView = (UIImageView *)tap.view;
    
    [JSTScanImage scanBigImageWithImageView:tapImageView];
}

- (void)comments
{
    _commentsBtn.selected = YES;
    _praiseBtn.selected = NO;
    [UIView animateWithDuration:0.3 animations:^ {
        CGRect newSelectViewFrame = _selectView.frame;
        newSelectViewFrame.origin.x = _commentsBtn.frame.origin.x;
        _selectView.frame = newSelectViewFrame;
    }];
    if (_delegate && [_delegate respondsToSelector:@selector(leftChange)]) {
        [_delegate leftChange];
    }
}

- (void)praise
{
    _commentsBtn.selected = NO;
    _praiseBtn.selected = YES;
    [UIView animateWithDuration:0.3 animations:^ {
        CGRect newSelectViewFrame = _selectView.frame;
        newSelectViewFrame.origin.x = _praiseBtn.frame.origin.x;
        _selectView.frame = newSelectViewFrame;
    }];
    if (_delegate && [_delegate respondsToSelector:@selector(rightChange)]) {
        [_delegate rightChange];
    }
}

- (void)changePersonPage
{
    if (_delegate && [_delegate respondsToSelector:@selector(changePersonPage)]) {
        [_delegate changePersonPage];
    }
}
@end
